﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class OrderDetails : System.Web.UI.Page
{
    SqlDataAdapter da;
    SqlConnection con;
    DataTable dtOrderDetails;
    DataTable dt;
    DataView dv;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {

            loadComboBox();
            if (!Session["getID"].Equals(""))
            {
                loadOrderDetailWhenOrderClick();
            }
            else loadOrderDetail();
        }
    }

    private void loadOrderDetail()
    {
        con = new SqlConnection();
        con.ConnectionString =
            WebConfigurationManager.ConnectionStrings["SqlStr"].ConnectionString;
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_SelectFromTblORderDetail";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        dt = new DataTable("OrderDetails");
        da.Fill(dt);
        gvOrderDetails.AutoGenerateSelectButton = true;
        gvOrderDetails.AllowPaging = true;
        gvOrderDetails.PageSize = 30;
        gvOrderDetails.DataSource = dt;
        gvOrderDetails.DataBind();
    }

    private void loadOrderDetailWhenOrderClick()
    {
        con = new SqlConnection();
        con.ConnectionString =
            WebConfigurationManager.ConnectionStrings["SqlStr"].ConnectionString;
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_SelectOrderDetailByID";
        SqlParameter para = new SqlParameter("@orderid", SqlDbType.Int);
        String temp = (String)Session["getID"];
        para.Value = temp;
        cmd.Parameters.Add(para);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        dt = new DataTable("OrderDetails");
        da.Fill(dt);
        gvOrderDetails.AutoGenerateSelectButton = true;
        //gvOrderDetails.AllowPaging = true;
        //gvOrderDetails.PageSize = 30;
        gvOrderDetails.DataSource = dt;
        gvOrderDetails.DataBind();
    }


    private void loadComboBox()
    {
        con = new SqlConnection();
        con.ConnectionString =
            WebConfigurationManager.ConnectionStrings["SqlStr"].ConnectionString;
        //load OrderId
        da = new SqlDataAdapter("Select * from Sales.Orders", con);
        dt = new DataTable("OrderID");
        da.Fill(dt);
        ddlOrderID.DataSource = dt;
        ddlOrderID.DataTextField = "orderid";
        ddlOrderID.DataValueField = "orderid";
        ddlOrderID.DataBind();

        //load productname;
        da = new SqlDataAdapter("select * from Production.Products", con);
        dt = new DataTable("ProductID");
        da.Fill(dt);
        ddlProduct.DataSource = dt;
        ddlProduct.DataTextField = "productname";
        ddlProduct.DataValueField = "productid";
        ddlProduct.DataBind();
    }
    protected void gvOrderDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        loadOrderDetail();
        gvOrderDetails.PageIndex = e.NewPageIndex;
        gvOrderDetails.DataBind();
    }
    protected void gvOrderDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow r = gvOrderDetails.SelectedRow;
        ddlOrderID.Text = r.Cells[1].Text;
        ddlProduct.Text = r.Cells[2].Text;
        txtUnitPrice.Text = Server.HtmlDecode(r.Cells[3].Text);
        txtQuantity.Text = Server.HtmlDecode(r.Cells[4].Text);
        txtDiscount.Text = Server.HtmlDecode(r.Cells[5].Text);
        btnAddDetail.Enabled = false;
        btnDeleteDetail.Enabled = true;
        btnUpdateDetail.Enabled = true;
        txtUnitPrice.Enabled = false;
        ddlOrderID.Enabled = false;
        ddlProduct.Enabled = false;
    }
    protected void txtReloadDetail_Click(object sender, EventArgs e)
    {
        loadOrderDetail();
    }

    protected void btnNewDetail_Click(object sender, EventArgs e)
    {
        btnAddDetail.Enabled = true;
        btnDeleteDetail.Enabled = false;
        btnUpdateDetail.Enabled = false;
        txtUnitPrice.Enabled = true;
        loadComboBox();
        clearFieldDetail();
        ddlProduct.Enabled = true;
        ddlOrderID.Enabled = true;
    }


    private void clearFieldDetail()
    {
        ddlOrderID.SelectedIndex = -1;
        ddlProduct.SelectedIndex = -1;
        txtUnitPrice.Text = "";
        txtDiscount.Text = "";
        txtQuantity.Text = "";
    }
    protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        addDetails();
        loadOrderDetail();
        Response.Write("<script>alert('Done!');</script>");
        clearFieldDetail();
        loadComboBox();
    }

    private void addDetails()
    {
        con = new SqlConnection();
        con.ConnectionString = WebConfigurationManager.ConnectionStrings["SqlStr"].ConnectionString;
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_InsertTotblOrderDetails";
            SqlParameter param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = ddlOrderID.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@productid", SqlDbType.Int);
            param.Value = ddlProduct.SelectedValue.ToString();
            cmd.Parameters.Add(param);

            param = new SqlParameter("@unitprice", SqlDbType.Money);
            param.Value = txtUnitPrice.Text.Trim();
            cmd.Parameters.Add(param);

            param = new SqlParameter("@qty", SqlDbType.SmallInt);
            param.Value = txtQuantity.Text.Trim();
            cmd.Parameters.Add(param);

            param = new SqlParameter("@discount", SqlDbType.Real);
            param.Value = txtDiscount.Text.Trim();
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            Response.Write(String.Format("<script>alert('{0}');</script>", ex.Message));
        }
        finally
        {
            con.Close();
        }
    }


    protected void btnUpdateDetail_Click(object sender, EventArgs e)
    {
        UpdateOrderDetail();
        loadOrderDetail();
        Response.Write("<script>alert('Done!');</script>");
        clearFieldDetail();
        loadComboBox();
    }

    private void UpdateOrderDetail()
    {

        con = new SqlConnection();
        con.ConnectionString = WebConfigurationManager.ConnectionStrings["SqlStr"].ConnectionString;
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_UpdatetblOrderDetails";
            SqlParameter param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = ddlOrderID.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@productid", SqlDbType.Int);
            param.Value = ddlProduct.SelectedValue.ToString();
            cmd.Parameters.Add(param);

            param = new SqlParameter("@unitprice", SqlDbType.Money);
            param.Value = txtUnitPrice.Text.Trim();
            cmd.Parameters.Add(param);

            param = new SqlParameter("@qty", SqlDbType.SmallInt);
            param.Value = txtQuantity.Text.Trim();
            cmd.Parameters.Add(param);

            param = new SqlParameter("@discount", SqlDbType.Real);
            param.Value = txtDiscount.Text.Trim();
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write(String.Format("<script>alert('{0}');</script>", ex.Message));

        }
        finally
        {
            con.Close();
        }
    }
    protected void btnDeleteDetail_Click(object sender, EventArgs e)
    {
        deleteOrderDetail();
        loadOrderDetail();
        Response.Write("<script>alert('Done!');</script>");
        loadComboBox();
        clearFieldDetail();
    }

    private void deleteOrderDetail()
    {

        con = new SqlConnection();
        con.ConnectionString = WebConfigurationManager.ConnectionStrings["SqlStr"].ConnectionString;
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_DeleteOrderDetail";
            SqlParameter param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = ddlOrderID.SelectedValue.ToString();
            cmd.Parameters.Add(param);

            param = new SqlParameter("@productid", SqlDbType.Int);
            param.Value = ddlProduct.SelectedValue.ToString();
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {

            Response.Write(String.Format("<script>alert('{0}');</script>", ex.Message));
        }
        finally
        {
            con.Close();
        }
    }
    protected void btnGoToOrderDetails_Click(object sender, EventArgs e)
    {
        Response.Redirect("Orders.aspx");
    }
}
